﻿using Abp.Application.Services.Dto;
using Abp.Domain.Repositories;
using Abp.Linq.Extensions;
using LpbPrj.Client.Patients.Dtos;
using System.Collections.Generic;
using System.Data.Entity;
using System.Linq;
using System.Threading.Tasks;

namespace LpbPrj.Client.Patients
{
    public class PatientService : ClientAppServiceBase, IPatientService
    {
        private readonly IRepository<Patient> _entityRepository;

        public PatientService(IRepository<Patient> entityRepository)
        {
            _entityRepository = entityRepository;
        }

        public async Task<PagedResultDto<PatientListDto>> GetPaged(GetPatientsPaged input)
        {
            var query = _entityRepository.GetAll().AsNoTracking()
                .WhereIf(input.CreatorUserId.HasValue, p => p.CreatorUserId.HasValue && p.CreatorUserId.Value == input.CreatorUserId.Value);

            var count = await query.CountAsync();
            var entityList = await query
                    .OrderByDescending(p => p.CreationTime)
                    .PageBy(input)
                    .ToListAsync();

            var entityListDtos = ObjectMapper.Map<List<PatientListDto>>(entityList);
            return new PagedResultDto<PatientListDto>(count, entityListDtos);
        }

        public async Task<PatientListDto> GetById(EntityDto input)
        {
            var entity = await _entityRepository.GetAll().AsNoTracking().FirstOrDefaultAsync(p => p.Id == input.Id);
            return ObjectMapper.Map<PatientListDto>(entity);
        }

        public async Task<PatientEditDto> GetForEdit(NullableIdDto input)
        {
            PatientEditDto editDto = null;

            if (input.Id.HasValue)
            {
                var entity = await _entityRepository.GetAll().AsNoTracking().FirstOrDefaultAsync(p => p.Id == input.Id.Value);
                editDto = ObjectMapper.Map<PatientEditDto>(entity);
            }
            else
            {
                editDto = new PatientEditDto();
            }

            return editDto;
        }

        public async Task<PatientListDto> CreateOrUpdate(PatientEditDto input)
        {
            int? id = input.Id;

            if (input.Id.HasValue)
            {
                await Update(input);
            }
            else
            {
                id = await Create(input);
            }
            CurrentUnitOfWork.SaveChanges();
            if (id.HasValue)
            {
                var entity = await _entityRepository.GetAll().AsNoTracking().FirstOrDefaultAsync(p => p.Id == id.Value);
                return ObjectMapper.Map<PatientListDto>(entity);
            }

            return null;
        }

        protected virtual async Task<int> Create(PatientEditDto input)
        {
            var entity = ObjectMapper.Map<Patient>(input);
            return await _entityRepository.InsertAndGetIdAsync(entity);
        }

        protected virtual async Task Update(PatientEditDto input)
        {
            var entity = await _entityRepository.GetAll().AsNoTracking().FirstOrDefaultAsync(p => p.Id == input.Id.Value);
            ObjectMapper.Map(input, entity);
            await _entityRepository.UpdateAsync(entity);
        }

        public async Task Delete(EntityDto input)
        {
            //TODO:删除前的逻辑判断，是否允许删除
            await _entityRepository.DeleteAsync(input.Id);
        }

        public async Task BatchDelete(List<int> input)
        {
            // TODO:批量删除前的逻辑判断，是否允许删除
            await _entityRepository.DeleteAsync(s => input.Contains(s.Id));
        }

    }
}
